<?php

namespace App\Http\Controllers\Admin;

use App\Http\Repository\SortRepository;
use App\Http\Servers\Admin\ImageServer;
use App\Http\Servers\Admin\SortServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class SortController extends CommonController
{
    //信息分类列表
    public function sort(Request $request, SortRepository $sortRepository, SortServer $sortServer, ImageServer $imageServer)
    {
        if ($request->has('del')) {
            $sort = \App\Sort::findorfail($request->del);

            if ($message = $sortServer->sofeVerify($request->del, $sort->author)) {
                $message['message'] = $message;
                return back()->with($message);
            }

            $result = $sort->delete();
            if ($result) {
                $imageServer->deleteImage($sort->image);

                $message['message'] = message(true, '删除成功', '信息分类 ' . $sort->name . ' 删除成功!');
            } else {
                $message['message'] = message(false, '删除失败', '信息分类 ' . $sort->name . ' 删除失败!');
            }
            return back()->with($message);
        }

        $data['chunk'] = \App\Chunk::findorfail($request->chunk);

        $data['sorts'] = $sortRepository->getSortsByX($request->chunk);

        return view('admin.sort.sort', $data);
    }

    //信息分类添加
    public function sort_add(Request $request, SortRepository $sortRepository, SortServer $sortServer, ImageServer $imageServer)
    {
        if ($request->isMethod('post')) {
            $this->validate($request, [
                'chunk' => 'required',
                'name' => 'required',
                'parent' => 'required',
                'theme' => 'required|sort_theme',
                'xindex' => 'required',
                'image' => 'image'
            ]);

            $create['chunk'] = $request->chunk;
            $create['name'] = $request->name;
            $create['parent'] = $request->parent;
            $pathprefix = \App\Sort::where('id', $request->parent)->value('path');
            $create['path'] = $pathprefix . $create['parent'] . ',';
            $create['xindex'] = $request->xindex;
            $create['theme'] = $request->theme;
            $create['author'] = \Auth::guard('admin')->user()->id;
            $create['describe'] = $request->describe;
            $create['user_write'] = $request->has('user_write') ? 1 : 0;
            $create['status'] = $request->has('status') ? 1 : 0;
            $create['simpledescribe'] = $request->has('simpledescribe') ? $request->simpledescribe : subtext(strip_tags($request->describe), config('app.simple_len'));

            if ($request->hasFile('image')) {
                $create['image'] = $imageServer->uploadImage($request->image, 'sort', 'sort');
            }

            $result = \App\Sort::create($create);
            if ($result) {
                $message['message'] = message(true, '添加成功', '添加分类 ' . $request->name . ' 成功!');
            } else {
                $message['message'] = message(false, '添加失败', '添加失败 ' . $request->name . ' 失败!');
            }
            return back()->with($message);
        }

        $data['chunk'] = \App\Chunk::findorfail($request->chunk);

        $data['sorts'] = $sortRepository->getSortsByX($request->chunk);

        $data['themes'] = $sortServer->getSortTheme();

        return view('admin.sort.sort_add', $data);
    }

    //信息分类编辑
    public function sort_edit(Request $request, SortRepository $sortRepository, SortServer $sortServer, ImageServer $imageServer)
    {
        if ($request->isMethod('post')) {

            $sort = \App\Sort::findorfail($request->id);
            if ($message = $sortServer->noPower($sort->author)) {
                $message['message'] = $message;
                return back()->with($message);
            }

            $this->validate($request, [
                'id' => 'required',
                'name' => 'required',
                'theme' => 'required|sort_theme',
                'xindex' => 'required',
                'image' => 'image'
            ]);

            $update['name'] = $request->name;
            $update['xindex'] = $request->xindex;
            $update['theme'] = $request->theme;
            $update['describe'] = $request->describe;
            $update['user_write'] = $request->has('user_write') ? 1 : 0;
            $update['status'] = $request->has('status') ? 1 : 0;
            $update['simpledescribe'] = $request->has('simpledescribe') ? $request->simpledescribe : subtext(strip_tags($request->describe), config('app.simple_len'));

            if ($request->hasFile('image')) {
                $update['image'] = $imageServer->uploadImage($request->image, 'sort', 'sort');
            }

            $result = \App\Sort::where('id', $request->id)->update($update);
            if ($result) {
                $message['message'] = message(true, '编辑成功', '编辑信息分类 ' . $request->name . ' 成功!');
            } else {
                $message['message'] = message(false, '编辑失败', '编辑信息分类 ' . $request->name . ' 失败!');
            }
            return back()->with($message);
        }

        $data['sort'] = \App\Sort::findorfail($request->id);
        $data['sort']['image'] = \App\Image::where('id', $data['sort']['image'])->value('path');

        $data['chunk'] = \App\Chunk::findorfail($request->chunk);

        $data['sorts'] = $sortRepository->getSortsByX($request->chunk);

        $data['themes'] = $sortServer->getSortTheme();

        return view('admin.sort.sort_edit', $data);
    }
}
